<h1>Resultados de la Búsqueda <b><?php echo $salida ?>-<?php echo $llegada ?></b></h1>

<?php include_partial('flashes') ?>

<?php 
    $ciudadinicio = $_POST['salida'];
	$ciudadllegada = $_POST['llegada'];    
	
    $coopbusqueda = Doctrine_Core::getTable('Cooperativa')->findAll();

	$respuesta = 0;
?>
<table class="table">
<?php
        foreach ($coopbusqueda as $cb):
            $q = Doctrine_Query::create()
                    ->from('regla r')
                    ->where('r.id_co=?', $cb->getIdCo());
            $regla = $q->fetchOne();
            $rutasbusquedas = Doctrine::getTable('ruta')->createQuery('r')
                    ->where('r.id_co = ?', $cb->getIdCo())
                    ->execute();
            foreach ($rutasbusquedas as $rb):
                if ($regla->getEspecial() == true) {
                    $c = 0;
                    $bandera = false;
                    $escalasespecialesbusquedas = Doctrine::getTable('escalaespecial')->createQuery('ee')
                            ->where('ee.id_ru = ?', $rb->getIdRu())
                            ->execute();

                    foreach ($escalasespecialesbusquedas as $eeb):
                        if (strtolower($eeb->getCiudad()) == strtolower($ciudadinicio)) {
                            $c++;
                        } else {
                            if ($c > 0) {
                                if (strtolower($eeb->getCiudad()) == strtolower($ciudadllegada)) {
                                    $bandera = true;
                                }
                            }
                        }
                    endforeach;
                    if ($bandera == true) {
                        $respuesta++;
                        ?>
<!--    Comienzo del codigo de mapa-->
    <tr><center>
        <form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false" name="form">

Salida: <input type="text" size="25" id="fromAddress" name="from"/>

Llegada: <input name="to" type="text" id="toAddress" size="25"/><br>

Seleccione el Idioma: <select id="locale" name="locale">
<option value="es" selected>Castellano</option>
<option value="en">English</option>

</select>

<input type="submit" name="Submit" value="Calcular Ruta Ingresada"/>

</form> 
<head>



<script src="http://maps.google.com/maps?file=api&amp;v=2&oe=ISO-8859-1;&amp;key=AIzaSyAvb2YacAfIyIoPc9jZFS4Lu0CVkfYQJUs"
type="text/javascript"></script> 







<script type="text/javascript">

var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
if (GBrowserIsCompatible()) { 
map = new GMap2(document.getElementById("mapa_ruta"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
gdir = new GDirections(map, document.getElementById("direcciones"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);

map.addControl(new GOverviewMapControl());
map.centerAndZoom(new GPoint(-3.99313, -79.20422), 8); 

setDirections("Loja", "Loja", "ec"); 

}
} 

function setDirections(fromAddress, toAddress, locale) {
gdir.load("from: " + fromAddress + " to: " + toAddress,
{ "locale": locale });
}

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("Dirección no disponible.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code); 
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code); 
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code); 
else alert("An unknown error occurred."); 
}

function onGDirectionsLoad(){ 
}


</script>


</head>

<body>


<div id="mapa_ruta" style="width: 710px; height: 300px; border: 4px solid #9999FF;"></div>

<div id="direcciones" style="width: 710px"></div>

<body onload="initialize()" onunload="GUnload()">

</body>
    </center>
<!--Aqui termina el codigo del mapa-->
    </tr>
                        <tr id="filascolor">
                            <td><b>Cooperativa</b></td>
                            <td colspan="2"><b>Ruta</b></td>
                        </tr>
                        <tr id="contextual">
                            <td><?php echo $cb->getNombre() ?></td>
                            <td colspan="2"><?php echo $rb->getSalida() ?>-<?php echo $rb->getLlegada() ?></td>
                        </tr>
                        <?php
                        $frecuenciasbusquedas = Doctrine::getTable('frecuencia')->createQuery('f')
                                ->where('f.id_ru = ?', $rb->getIdRu())
                                ->execute();
                        ?>
                        <tr id="filascolor">
                            <td>Horarios</td> 
                            <td colspan="2">Tipo</td> 
                        </tr>
                        <?php
                        foreach ($frecuenciasbusquedas as $fb):
                            ?>
                            <tr id="contextual">
                                <td><?php echo $fb->getHora() ?></td>
                                <?php if ($fb->getEspecial()) { ?>
                                    <td colspan="2">Especial</td>    
                                <?php } else { ?>
                                    <td colspan="2">Normal</td>    
                                <?php }
                                ?>                                                
                            </tr>
                            <?php
                        endforeach;
                        ?>
                        <tr id="filascolor">
                            <td>Escalas</td>
                            <td>Valor Normal</td>
                            <td>Valor Especial</td>
                        </tr>
                        <?php
                        $precon = 0;
                        foreach ($escalasespecialesbusquedas as $eeb):
                            $precon++;
                            if ($precon > 1) {
                                ?>
                                <tr id="contextual">
                                    <td><?php echo $eeb->getCiudad() ?></td>
                                    <td>$ <?php echo $eeb->getCostonormal() ?></td>
                                    <td>$ <?php echo $eeb->getCostoespecial() ?></td>
                                </tr>
                                <?php
                            }

                        endforeach;
                    }
                } else {
                    //rutas normales
                    $c = 0;
                    $bandera = false;
                    $escalasnormalesbusquedas = Doctrine::getTable('escalanormal')->createQuery('ee')
                            ->where('ee.id_ru = ?', $rb->getIdRu())
                            ->execute();

                    foreach ($escalasnormalesbusquedas as $eeb):
                        if (strtolower($eeb->getCiudad()) == strtolower($ciudadinicio)) {
                            $c++;
                        } else {
                            if ($c > 0) {
                                if (strtolower($eeb->getCiudad()) == strtolower($ciudadllegada)) {
                                    $bandera = true;
                                }
                            }
                        }
                    endforeach;
                    if ($bandera == true) {
                        $respuesta++;
                        ?>
                        <tr id="filascolor">
                            <td><b>Cooperativa</b></td>
                            <td colspan="2"><b>Ruta</b></td>
                        </tr>
                        <tr id="contextual">
                            <td><?php echo $cb->getNombre() ?></td>
                            <td colspan="2"><?php echo $rb->getSalida() ?>-<?php echo $rb->getLlegada() ?></td>
                        </tr>
                        <?php
                        $frecuenciasbusquedas = Doctrine::getTable('frecuencia')->createQuery('f')
                                ->where('f.id_ru = ?', $rb->getIdRu())
                                ->execute();
                        ?>
                        <tr id="filascolor">
                            <td>Horarios</td> 
                            <td colspan="2">Tipo</td> 
                        </tr>
                        <?php
                        foreach ($frecuenciasbusquedas as $fb):
                            ?>
                            <tr id="contextual">
                                <td><?php echo $fb->getHora() ?></td>
                                <?php if ($fb->getEspecial()) { ?>
                                    <td colspan="2">Especial</td>    
                                <?php } else { ?>
                                    <td colspan="2">Normal</td>    
                                <?php }
                                ?>                                                
                            </tr>
                            <?php
                        endforeach;
                        ?>
                        <tr id="filascolor">
                            <td>Escalas</td>
                            <td colspan="2">Valor Normal</td>

                        </tr>
                        <?php
                        $precon = 0;
                        foreach ($escalasnormalesbusquedas as $eeb):
                            $precon++;
                            if ($precon > 1) {
                                ?>
                                <tr id="contextual">
                                    <td><?php echo $eeb->getCiudad() ?></td>
                                    <td colspan="2">$ <?php echo $eeb->getCostonormal() ?></td>

                                </tr>
                                <?php
                            }

                        endforeach;
                    }
                }

            endforeach;

        endforeach;
        if ($respuesta == 0) {
            ?>
            <tr id="contextual">
                <td>Sin resultados</td>
            </tr>
        <?php
        }
?>
</table>

<div style="text-align:center">
    <a href="<?php echo url_for('inicio/index')?>">
        <button type="submit" class="button2 button-azul" title="Regresar al Inicio">
            <div class="spritesatras" id="atras_jpg"></div>Regresar a Inicio</button></a>
</div>